﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EM.Repository.utility;
using System.Data.SqlClient;
using System.Data;

namespace EM.definition.finance.CreditCard
{
    public partial class default_edit : System.Web.UI.Page
    {
        string sTask = "";
        public int CREDITCARDID = 0;

        FillCombo Fc = new FillCombo();

        protected void Page_Load(object sender, EventArgs e)
        {

            Authentication ua = new Authentication();
            ua.CheckRolePageAuthentication(13, Authentication.RedirectionURLTypes.TimeOutPage);

            sTask = Request.QueryString["task"].ToString();
            Int32.TryParse(Request.QueryString["ID"], out CREDITCARDID);
            if (!IsPostBack)
            {
                Fc.Year(ddlEXPIREYEAR, "", true);
                Fc.Month(DDLEXPIREMONTH, "", true);
                Fc.LookUpValue(ddlCUTOFDATEDAY, "1", false, "CREDIT_CARD_CUT_OF_DATE", "TR");
                Fc.Bank(ddlBANK, true, 0, false);
                Fc.User(ddlUser, "0", true, Convert.ToInt32(Session["BRANCHID"]));
                switch (sTask)
                {
                    case "Insert":
                        {
                            btnSave.CommandArgument = "Insert";
                            break;
                        }
                    case "Update":
                        {
                            btnSave.CommandArgument = "Update";
                            GetData();

                            break;
                        }

                    case "Delete":
                        {
                            btnSave.CommandArgument = "Delete";
                            lblSave.Text = "Sil";
                            GetData();
                            break;
                        }
                }
            }
        }

        private void GetData()
        {

            DbHelper srv = new DbHelper();
            SqlCommand cmd = new SqlCommand();

            string sSQL = @" SELECT C.CREDITCARDID, C.CREDITCARDNAME, C.BANKID,C.CUTOFDATE,C.USERID,
                             C.EXPIREYEAR, C.EXPIREMONTH,C.ACTIVEFLG, C.CREDITCARDNO
                            FROM TBLCREDITCARD C
                            WHERE C.CREDITCARDID = @CREDITCARDID";
            cmd.CommandText = sSQL;
            cmd.Parameters.Add("CREDITCARDID", SqlDbType.Int).Value = CREDITCARDID;
            DataTable dt = srv.GetDataTable(cmd, "");

            if (dt.Rows.Count > 0)
            {
                lblCreditCardId.Text = dt.Rows[0]["CREDITCARDID"].ToString();
                txtCREDITCARDNAME.Text = dt.Rows[0]["CREDITCARDNAME"].ToString();
                Fc.Bank(ddlBANK, true, Convert.ToInt32(dt.Rows[0]["BANKID"]), false);
                ddlUser.SelectedValue = dt.Rows[0]["USERID"].ToString();
                //Fc.Year(ddlEXPIREYEAR, dt.Rows[0]["EXPIREYEAR"].ToString(), true);
                ddlEXPIREYEAR.SelectedValue = dt.Rows[0]["EXPIREYEAR"].ToString();
                //Fc.Month(DDLEXPIREMONTH, dt.Rows[0]["EXPIREMONTH"].ToString(), true);
                DDLEXPIREMONTH.SelectedValue = dt.Rows[0]["EXPIREMONTH"].ToString();
                Fc.LookUpValue(ddlCUTOFDATEDAY, dt.Rows[0]["CUTOFDATE"].ToString(), false, "CREDIT_CARD_CUT_OF_DATE", "TR");
                chkActive.Checked = dt.Rows[0]["ACTIVEFLG"].ToString() == "Y";
                txtCREDITCARDNO.Text = dt.Rows[0]["CREDITCARDNO"].ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            DbHelper srv = new DbHelper();
            SqlCommand cmd = new SqlCommand();

            string sSQL = "";

            if (sTask == "Insert")
            {
                sSQL = " INSERT INTO TBLCREDITCARD (BANKID, CREDITCARDNAME, USERID,CREDITCARDNO, ACTIVEFLG, CREUSERID, MODUSERID, CREDATE, MODDATE, EXPIREYEAR, EXPIREMONTH, CUTOFDATE, WORKGROUPID) ";
                sSQL += "SELECT @BANKID, @CREDITCARDNAME, @USERID,@CREDITCARDNO, @ACTIVEFLG, @CREUSER, @CREUSER, GetDate(), GetDate(),@EXPIREYEAR, @EXPIREMONTH, @CUTOFDATE, @WORKGROUPID";
            }
            else if (sTask == "Update")
            {
                sSQL = " UPDATE TBLCREDITCARD SET";
                sSQL += "  BANKID = @BANKID, ";
                sSQL += "  CREDITCARDNAME = @CREDITCARDNAME, ";
                sSQL += "  USERID = @USERID, ";
                sSQL += "  CREDITCARDNO = @CREDITCARDNO, ";
                sSQL += "  ACTIVEFLG = @ACTIVEFLG, ";
                sSQL += "  MODUSERID = @MODUSER, ";
                sSQL += "  MODDATE = GetDate(), ";
                sSQL += " EXPIREYEAR = @EXPIREYEAR, EXPIREMONTH = @EXPIREMONTH, ";
                sSQL += " CUTOFDATE = @CUTOFDATE, WORKGROUPID = @WORKGROUPID ";
                sSQL += " WHERE CREDITCARDID = @CREDITCARDID ";
            }
            else if (sTask == "Delete")
            {
                sSQL = " DELETE FROM TBLCREDITCARD WHERE CREDITCARDID = @CREDITCARDID ";
            }

            cmd.CommandText = sSQL;
            cmd.Parameters.Add("@CREDITCARDID", SqlDbType.Int).Value = CREDITCARDID;
            cmd.Parameters.Add("@BANKID", SqlDbType.Int).Value = ddlBANK.SelectedValue;
            cmd.Parameters.Add("@CREDITCARDNAME", SqlDbType.NVarChar).Value = txtCREDITCARDNAME.Text;
            if (!(bool)HttpContext.Current.Session["UserIsAnAdmin"])
                cmd.Parameters.AddWithValue("@USERID", Session["USERID"]);
            else
                cmd.Parameters.AddWithValue("@USERID", ddlUser.SelectedValue);
            cmd.Parameters.Add("@CREDITCARDNO", SqlDbType.NVarChar).Value = txtCREDITCARDNO.Text;
            cmd.Parameters.Add("@EXPIREYEAR", SqlDbType.Int).Value = ddlEXPIREYEAR.SelectedValue;
            cmd.Parameters.Add("@EXPIREMONTH", SqlDbType.Int).Value = DDLEXPIREMONTH.SelectedValue;

            if (chkActive.Checked)
                cmd.Parameters.Add("@ACTIVEFLG", SqlDbType.NVarChar).Value = "Y";
            else
                cmd.Parameters.Add("@ACTIVEFLG", SqlDbType.NVarChar).Value = "N";


            cmd.Parameters.Add("@CREUSER", SqlDbType.NVarChar).Value = Session["USERID"].ToString();
            cmd.Parameters.Add("@MODUSER", SqlDbType.NVarChar).Value = Session["USERID"].ToString();
            cmd.Parameters.Add("@WORKGROUPID", SqlDbType.NVarChar).Value = Session["WORKGROUPID"].ToString();
            cmd.Parameters.AddWithValue("@CUTOFDATE", ddlCUTOFDATEDAY.SelectedValue);

            srv.ExecuteNonQuery(cmd);

            string strScript = "window.opener['rgMaster'].AjaxRequest('rgMaster', 'Rebind'); window.close();";
            ClientScript.RegisterStartupScript(typeof(string), "", strScript.ToString(), true);
        }
    }
}